לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

Similar documents
לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

מבוא לתכנות תוכנית שעור מס. 1 1 דר' דרור טובי, המרכז האוניברסיטאי אריאל בשומרון.

מבוא לתכנות ב- JAVA מעבדה 2

קורס תכנות שיעור שני: שימוש במשתנים,

תוכנה 1 סמסטר א' תשע"א

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

תרגול 3 מערכים ופונקציות

מבוא לתכנות ב- JAVA מעבדה 4

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

תוכנה 1 טיפוסי השפה טיפוסים לא פרימיטיביים הטיפוסים הפרימיטיביים מחרוזות המרה למספרים תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות

תוכנה 1 תרגול 2: מערכים ומבני בקרה

מבוא לתכנות ב- JAVA תרגול 7

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> }

מבוא לתכנות ב- JAVA תרגול 5. Ipc161- practical session 5

מבוא לתכנות ב- JAVA תרגול 6

הנכות 1 םוכיס לוגרת 13 1

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen

מבוא למדעי המחשב תירגול 3:

Engineering Programming A

מבוא למדעי המחשב תירגול 2: מבוא למדעי המחשב מ' - תירגול 2

תרגול מספר 3: מערכים

תרגול 3 מערכים ופונקציות

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

תור שימושים בעולם התוכנה

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

Introduction to Programming in C תרגול 8

Amortized Analysis, Union-Find,

מבוא למדעי המחשב הרצאה 2: ראשונים בשפת C

רזח יליגרתו םי יראני ב ם

Relational and Logical Statements

מבוא למדעי המחשב תרגול 5: לולאות ומערכים

קורס תכנות שיעור שישי: מחרוזות, מצביעים

רשימות דילוגים Skip Lists

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1

תוכנה 1 תרגול מספר 13

הנכות 1 םוכיס לוגרת 13 1

תוכנה 1 תרגול מספר 13

תוכנה 1 * לא בהכרח בסדר הזה

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

שאלה 1 מהו הפלט של התוכנית הבאה:

עקרונות שפות תכנות 2016 תרגול 8 Type Inference System. Substitution Interpreter.

תרגילים ופתרונות בשפת - C הסתעפויות

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

מדעי המחשב 2 יחידות לימוד פתרון בחינת הבגרות פרק א. I x > a. פתרון 2: משפט switch

Graph Database, think different!

Computer Programming A תרגול 9

גירסה

תרגול 6 רקורסיה ותכנות מונחה עצמים

Exams questions examples

Algorithms. Intro2CS week 5

Practical Session - Heap

פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות

Practical Session No. 14 Topological sort,amortized Analysis

הנכות 1 םוכיס לוגרת 14 1

חוברת תרגילים לתרגול יסודות התכנות

Programming in C תרגול 8

Practical Session 2 Constants Don t Matter!!!

$ gcc check.c. $ a.out. $ gcc check.c -o check. $ check. $ gcc -Wall check.c -o check. #include <stdio.h>

תוכנה 1 * לא בהכרח בסדר הזה

קורס תכנות כתובות בזיכרון כתובות בזכרון מצביעים וכתובות מצביעים וכתובות שיעור שביעי: מבנים, הקצאת זיכרון דינאמית האופרטור &

קורס תכנות רשימה מקושרת דוגמה: חיפוש מעבר על רשימה דוגמא: שחרור רשימה מקושרת דוגמא: הוספת אברים שלא בהתחלה

עמוד 1 (תאריך ( âùéä ער äìàù בכל השאלות ניתן להניח שהקלט תקין. 100 íåëñ חורף :

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 ומחלקות פנימיות

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C

Programming for Engineers in Python

Programming for Engineers in Python

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

עבודה עם STL. )Dmitry Korolev

Computer Programming Summer 2017

סעיף ב )11 נקודות( public synchronized void advance(staircase staircase) throws InterruptedException staircase!

עצים. מבני נתונים Iterators רשימות מקושרות עצים "רגילות" רקורסיביות

מבוא לתכנות ב- JAVA מעבדה 3. Ipc161-lab3

הנכות 1 םוכיס לוגרת 13 1

Introduction to OOP. Contents:

תרגול 12. Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf("bla\n"); 8. return 0; 9. }

תכנות מתקדם בשפת C משתנים

תוך כדי ריצה יכולה לבוא אירוע שמפסיק את מהלך הריצה )או באופן לא רצוני כגון שיחה נכנסת או באופן רצוני על ידי זימון )pauseapp()

Tutorial 10. Introduction to C++ שימו

כתבו קוד ב- 3 קבצי ה hpp (כתבו כהערה את שם הקובץ מעל) כך שהקוד יהיה תקין ובסגנון טוב. אין חובה

Simple Web Service. namespace MyService { public class Service1 : System.Web.Services.WebService {

LaserJet Enterprise M506

else if (p.getinfo().getvotes() < c1.getvotes()) { c1 = p.getinfo()

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform

2. (לדוגמא: יחידת הספריה (my_unit.c. #include "my_unit.c" היחידה (חלק ה-.(prototype לדוגמא:. my_unit.h הכותר. הוראת מדעי המחשב, אוניברסיטת תל-אביב

Introduction to OOP. Contents:

מבוא למדעי המחשב תרגול 13: עצים בינאריים

דף הדרכה ליצירת שרת/ לקוח עם GUI

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה

תוכנה 1. תרגול 1: סביבת העבודה ומבוא ל- Java

Transcription:

מבוא לתיכנות עם MATLAB 234127 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 2013 Based on slides of Dr. Eran Eden, Weizmann 2008 1

>>g = [89 91 80 98]; >>p = [3 5 5.5 4]; ביטויים לוגיים דוגמא: תקינות אקדמית ממוצע %%%%%%% >>sum(g.*p)/sum(p) 88.8000 אחוזי הצלחה %%%%%%% >>sum(p.*(g>=55))/sum(p) תקינות אקדמית %%%%%%% 1 >>sum(g.*p)/sum(p) >= 65 && sum(p.*(g>=55))/sum(p) > 2/3 1 2

Operator Precedence Parentheses () Transpose (. ), power (.^), complex conjugate transpose ( ), matrix power (^) Unary plus (+), unary minus ( ), logical negation (~) Multiplication (.*), right division (./), left division(.\), matrix multiplication (*), matrix right division (/), matrix left division (\) Addition (+), subtraction ( ) Colon operator (:) Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=) Element wise AND (&) Element wise OR ( ) Short circuit AND (&&) Short circuit OR ( ) 3

Boolean logic What is boolean logic? Why do we need boolean logic? Any expression in Matlab is associated with a true/false value. 0 false 1 true (Actually everything besides 0 is true by default) 4

Boolean logic Examples: >> x = 4; >> y = 0; >> z = -2 * 2; Expression Value x true y false z true x + z false x * y false 5

Boolean logic You can create variables with logical values Examples: >> a = true a = 1 >> b = false b = 0 >> a + b 1 >> a * b 0 6

Relational operators What are relational operators? < (Less than) > (Greater than) <= (Less than or equal to) >= (Greater than or equal to) == (Equal to) ~= (Not equal to) Why do we need relational operators? Watch out! Common mistakes: (1)Don t confuse = and == (2)<= and NOT =< (3)>= and NOT => The purpose of the relational and logical operations is to provide true/false answers to questions that compare between objects 7

Relational operators Examples: >> x = 5; >> y = 6; 10 < x < 20 0 < 20 Expression Value x < y true x <= y true 4 > 5 false x == 5 true x ~= y true x + 1 y == 0 true 10 < x < 20 true 1 8

Relational operators One of the powerful things about Matlab is that relational operations also work on arrays Example >> x = 1 : 8; x = 1 2 3 4 5 6 7 8 >> y = 8 : -1 : 1; y = 8 7 6 5 4 3 2 1 >> bool_res = x > y bool_res = 0 0 0 0 1 1 1 1 9

Relational operators A bit more complicated example: x = 1 : 0.1 : 8 * pi; plot(x, sin(x)); plot(x, (sin(x) > 0).* sin(x), 'r'); 10

Logical operators What are logical operators? They provide a way to combine or negate relational expressions Logical operators Description && AND OR ~ NOT 11

Logical operators We can specify each logical operator using a truth table AND OR NOT x y x && y x y x y x ~x 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 12

Logical operators In a previous slide we saw that >> x = 5 >> 10 < x < 20 1 We can use logical operators to fix this. Write a statement that is true only if x is bigger than 10 AND smaller than 20. >> x = 5 >> (10 < x) && (x < 20) 0 This statement is true in matlab!#?@ 13

Logical operators More examples: >> x = 1; y = 2; z = 3; Expression Value 4 && -5 true ~0 true y > z && y > x false ~( y > z y > x ) false (1 <= x) ~((x * z / 3) < y) (((y + z) 4 * x) > 2) (y > 10) && ~(1 <= x + y) && (x^3 + y^3 > z ^2) true false 14

Controlling precedence using parentheses >> x = false; >> y = true; >> x && y true 1 >> x && (y true) 0 15

Logical operators Logical operators can be easily applied to arrays: Logical operator & Description Element-wise AND Element-wise OR ~ Not Common mistakes Please notice: The difference between & and && The difference between and 16

Logical operators Example of element-wise logical operators >> x = 1 : 8; x = 1 2 3 4 5 6 7 8 >> y = 8 : -1 : 1; y = 8 7 6 5 4 3 2 1 >> bool_res = x > y bool_res = 0 0 0 0 1 1 1 1 >> (x > y) & (y <= 2) 0 0 0 0 0 0 1 1 >> (x < 2) (y < 2) 1 0 0 0 0 0 0 1 >> bool_res_neg = ~(x > y) bool_res_neg = 1 1 1 1 0 0 0 0 17

קדימויות בין אופרטורים כשלא משתמשים בסוגריים, על האופרטור. לאופרטור & יש קדימות פעולות חשבון קודמות לאופרטורים של השוואה, קודמים לאופרטורים לוגיים. דוגמא: c<0 L = a>3 & b^2<8 ככלל, מומלץ להשתמש בסוגריים כדי לקבוע את סדר ביצוע הפעולות: L = (a>3)&((b^2<8) (c<0)) ואלו 18

חישוב עצל של אופרטורים לוגיים כדי לחסוך בזמן ריצה, קיימים במטלב אופרטורים לוגיים && ו-, העוצרים את החישוב ברגע שערכו אינו ניתן לשינוי. (a>3) &&((b^2<8) (c<0)) אם a אינו גדול מ 3, ערכו של הביטוי הוא בהכרח,(false) 0 והחישוב (( b^2<8) (c<0 ))לא יתבצע. 19 הערה: חישוב עצל עלול לשנות את סדר הקדימויות הסטנדרטי: >> (a>3) && (b^2<8) (c<0 ) 0 >> (a>3) & (b^2<8) (c<0 ) 1

Operator Precedence Parentheses () Transpose (. ), power (.^), complex conjugate transpose ( ), matrix power (^) Unary plus (+), unary minus ( ), logical negation (~) Multiplication (.*), right division (./), left division(.\), matrix multiplication (*), matrix right division (/), matrix left division (\) Addition (+), subtraction ( ) Colon operator (:) Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=) Element wise AND (&) Element wise OR ( ) Short circuit AND (&&) Short circuit OR ( ) 20

ביטויים לוגיים דוגמא: תקינות אקדמית >>g = [89 91 80 98]; >>p = [3 5 5.5 4]; ממוצע %%%%%%% >>sum(g.*p)/sum(p) 88.8000 אחוזי הצלחה %%%%%%% >>sum(p.*(g>=55))/sum(p) 1 תקינות אקדמית %%%%%%% >>sum(g.*p)/sum(p) >= 65 && sum(p.*(g>=55))/sum(p) > 2/3 סדר פעולות 2 1 4 3 5 13 8 7 6 10 9 12 11 21

המרה אוטומטית בין מספרים לערכים לוגיים כאשר מטלב מבצעת פעולה חשבונית או לוגית, היא ממירה באופן אוטומטי את הערכים עליהם מבוצעת הפעולה כך שיתאימו לפעולה: >> (1==1) + (2<=3) 2 >> 5 0 1 >> ( (5+1)*2) & (4-4) 0 >> (2*(2==2))^2 + 3*true + false + 7 14 22

Array indexing using logical operators Example: Get the IDs of all the employees who got a salary above 5000 and worked less than 10 days a month. >> salary = [1000, 7000, 6000, 3000, 8000]; >> work_days = [20, 8, 28, 5, 7]; >> ids = [100007, 100008, 100009, 100010, 100011]; salary > 5000 0 1 1 0 1 work_days < 10 0 1 0 1 1 (salary > 5000) & (work_days < 10) 0 1 0 0 1 >> ids((salary > 5000) & (work_days < 10)) 100008 100011 23

Sub-array searching The find operation >> x = [2 8 7 6 4 2 3]; >> find(x == 2) 1 6 >> find(x > 3) 2 3 4 5 Finds indices of all values equal to 2 Finds indices of all values bigger than 3 How do we get the values in x that are larger than 3? How do we get the value in x that is larger than 3 and is most to the right? 24

פונקצית find שימוש שכיח במשתנים לוגיים: שליפת ערכים המקיימים תנאי נתון. >> x = [2 8 7 6 4 2 3]; >> L = x > 3 L = 0 1 1 1 1 0 0 >> indices = find(l) indices = 2 3 4 5 פונקצית find מקבלת מערך לוגי L, ומחזירה מערך המכיל את האינדקסים i עבורם L(i)=true 25

פונקצית find שימוש שכיח במשתנים לוגיים: שליפת ערכים המקיימים תנאי נתון. כאשר N הוא מערך מספרים, find(n) תחזיר את האינדקסים של המספרים השונים מאפס ב N )למה?( >> N=[1-3 -4 0 0 8 9 0 8]; >> find(n) 1 2 3 6 7 9 26

פונקצית find על מערכים רב ממדיים כאשר M הוא מערך רב מימדי, find(m) תחזיר את המיקום של האברים שאינם 0 במערך הפריסה >> M=[1-3 -4; 0 0 8; 9 0 8] M = 1-3 -4 0 0 8 9 0 8 >> find(m) % use to print row rather % then column vector 1 3 4 7 8 9 27

פונקצית find על מערכים רב ממדיים אם מעוניינים לקבל את מספרי השורות והעמודות של אברים אלו, צריך להשתמש ב: >> M=[1-3 -4; 0 0 8; 9 0 8] M = 1-3 -4 0 0 8 9 0 8 >> [rows, cols]=find(m); >> rows 1 3 1 1 2 3 >> cols 1 1 2 3 3 3 28

מיעון באמצעות מערכים לוגיים על ידי שימוש במערכים לוגיים ובחוקי המיעון שלמדנו, ניתן לבחור ממערך נתון ערכים המקיימים תנאי לוגי: >> x = [2 8 7 6 4 2 3]; >> x(x>3) 8 7 6 4 2 שקול ל x(find(x>2)) מה יודפס על המסך אם נוסיף את הפקודות: >> b=[2-1 -3 6 0]; >> x(b<0)??? Logic array [0 1 1 1 1 0 0] Num array [2 3 4 5] 29

מיעון באמצעות מערכים לוגיים >> s='moshe'; >> t=s(s<'k') t = Mhe חוקי מיעון דומים תקפים גם למחרוזות: 30

פונקציות לוגיות מיוחדות xor([1 1 0 0], [0 1 0 1]) 1 0 0 1 עוד אופרטור לוגי: xor x y xor(x,y) 0 0 0 0 1 1 1 0 1 1 1 0 אופרטורים לוגיים על כל אברי מערך נתון: any, all all(grades>55) אוסף גדול של פרדיקטים: isprime, islogical, isempty, 31

דוגמה )ממבחן!( נתונים שני מערכי שורה a ו- b )לא ריקים(, איך ניתן לבדוק האם יש שני איברים ב- a שגדולים מכל איברי b? פתרון: A = repmat(a, [length(b), 1]); B = repmat(b,[1, length(a)]); sum(sum(a > B) == length(b))==2 32